package com.bwie.controller;

import com.bwie.espojo.ESShop;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @description:
 * @author: ykk
 * @create: 2024-10-08 09:47
 **/
@RestController
@RequestMapping("/esshop")
public class ESController {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;


    @PostMapping("/addEs")
    public R addEs(){
        IndexOperations indexOps = elasticsearchRestTemplate.indexOps(ESShop.class);
        indexOps.create();
        Document mapping = indexOps.createMapping();
        boolean b = indexOps.putMapping(mapping);
        if(b){
            return R.OK("创建成功");
        }else{
            return R.ERROR(-1,"创建失败");
        }
    }

    @PostMapping("/list")
    public R list(@RequestBody PageInfoVo pageInfoVo){
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if(!StringUtils.isEmpty(pageInfoVo.getKeyWord())){
            boolQueryBuilder.must(
                    QueryBuilders.matchQuery("shopNumber",pageInfoVo.getKeyWord())
            );
        }
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);

        if(!StringUtils.isEmpty(pageInfoVo.getBeginTime()) || !StringUtils.isEmpty(pageInfoVo.getEndTime())){
            boolQueryBuilder.must(
              QueryBuilders.rangeQuery("shopTime")
                      .format("yyyy-MM-dd HH:mm:ss")
                      .timeZone("GMT+8")
                      .gte(pageInfoVo.getBeginTime())
                      .lte(pageInfoVo.getEndTime())
            );
        }
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);

        Integer pageNum=1;
        Integer pageSize=3;
        if(Objects.nonNull(pageInfoVo.getPageNum())){
            pageNum=pageInfoVo.getPageNum()-1;
        }

        if(Objects.nonNull(pageInfoVo.getPageSize())){
            pageSize=pageInfoVo.getPageSize();
        }

        PageRequest of = PageRequest.of(pageNum, pageSize);
        nativeSearchQueryBuilder.withPageable(of);

        SearchHits<ESShop> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), ESShop.class);
        ArrayList<ESShop> arrayList = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        long totalHits = search.getTotalHits();
        if(totalHits>0){
            List<SearchHit<ESShop>> searchHits = search.getSearchHits();
            searchHits.forEach(item->{
                ESShop content = item.getContent();
                arrayList.add(content);
            });
            hashMap.put("arrayList",arrayList);
            hashMap.put("total",totalHits);
        }
        return R.OK(hashMap);
    }
}
